// Copyright 2007-2009, Stanford University. All Rights Reserved.
// Copyright 2010-2011, Dependable Systems Lab, EPFL. All Rights Reserved.
// Copyright 2012, Google Inc. All Rights Reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimers.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimers
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the names of the klee Team, Stanford University, nor the
// names of its contributors may be used to endorse or promote
// products derived from the software without specific prior
// written permission. Neither the names of the Dependable Systems
// Laboratory, EPFL, nor the names of its contributors may be used to
// endorse or promote products derived from the software without prior
// written permission. Neither the names of Google Inc. nor the names of
// its contributors may be used to endorse or promote products derived from
// the software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef SRC_EXPR_ASSIGNMENT_H_
#define SRC_EXPR_ASSIGNMENT_H_

#include <map>
#include <vector>

#include "expr/ExprEvaluator.h"
#include "util/Ref.h"

// FIXME: Rename?

namespace seven_summits {
namespace expr {

class Array;
using util::ref;

class Assignment {
public:
  typedef std::map<const Array*, std::vector<unsigned char> > bindings_ty;

  bool allowFreeValues;
  bindings_ty bindings;

public:
  Assignment(bool _allowFreeValues=false)
    : allowFreeValues(_allowFreeValues) {}
  Assignment(std::vector<const Array*> &objects,
             std::vector< std::vector<unsigned char> > &values,
             bool _allowFreeValues=false)
    : allowFreeValues(_allowFreeValues){
    std::vector< std::vector<unsigned char> >::iterator valIt =
      values.begin();
    for (std::vector<const Array*>::iterator it = objects.begin(),
           ie = objects.end(); it != ie; ++it) {
      const Array *os = *it;
      std::vector<unsigned char> &arr = *valIt;
      bindings.insert(std::make_pair(os, arr));
      ++valIt;
    }
  }

  ref<Expr> evaluate(const Array *mo, unsigned index) const;
  ref<Expr> evaluate(ref<Expr> e);

  template<typename InputIterator>
  bool satisfies(InputIterator begin, InputIterator end);
};

class AssignmentEvaluator : public ExprEvaluator {
  const Assignment &a;

protected:
  ref<Expr> getInitialValue(const Array &mo, unsigned index) {
    return a.evaluate(&mo, index);
  }

public:
  AssignmentEvaluator(const Assignment &_a) : a(_a) {}
};

/***/

inline ref<Expr> Assignment::evaluate(const Array *array,
                                      unsigned index) const {
  bindings_ty::const_iterator it = bindings.find(array);
  if (it!=bindings.end() && index<it->second.size()) {
    return ConstantExpr::alloc(it->second[index], Expr::Int8);
  } else {
    if (allowFreeValues) {
      return ReadExpr::create(UpdateList(array, 0),
                              ConstantExpr::alloc(index, Expr::Int32));
    } else {
      return ConstantExpr::alloc(0, Expr::Int8);
    }
  }
}

inline ref<Expr> Assignment::evaluate(ref<Expr> e) {
  AssignmentEvaluator v(*this);
  return v.visit(e);
}

template<typename InputIterator>
inline bool Assignment::satisfies(InputIterator begin, InputIterator end) {
  AssignmentEvaluator v(*this);
  for (; begin!=end; ++begin)
    if (!v.visit(*begin)->isTrue())
      return false;
  return true;
}

}  // namespace expr
}  // namespace seven_summits

#endif  // SRC_EXPR_ASSIGNMENT_H_
